package org.example.entity;

import org.example.service.IPrint;
import org.example.util.DateUtil;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Random;

/**
 * @author TonySong
 * 电影票：父类
 * @date 2025/7/28 0028
 * @time 16:27
 */
public class Ticket implements Serializable, IPrint {
    private Movie movie;
     private String seat;

    public String getSeat() {
        return seat;
    }

    public void setSeat(String seat) {
        this.seat = seat;
    }

    public String createSeat(){
        Random ran=new Random();
        int m=ran.nextInt(20)+1;
        int n=ran.nextInt(30)+1;
        return m+"排"+n+"号";
    }

    public Ticket() {
        this.seat = this.createSeat();
    }

    public Ticket(Movie movie) {
        this();
        this.movie = movie;
    }

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }

    public int calc(){
        //普通票的价格：原价
        return this.movie.getPrice();
    }

    @Override
    public String toString() {
        return "Ticket{" +
                "movie=" + movie +
                ", seat='" + seat + '\'' +
                ", price=" + calc() +'\''+
                '}';
    }

    @Override
    public void print() throws IOException {
        //考虑文件重名的问题：时间戳
        String fname= DateUtil.createTime();
        System.out.println(fname);
        PrintWriter writer =new PrintWriter(new FileWriter("普通票"+fname+".txt"));
        writer.print("========================夜不归宿电影院欢迎您====================================\r\n");
        writer.print("电影名称："+this.movie.getName()+"时间："+ DateUtil.showDate(this.movie.getPlayTime())+"\r\n");
        writer.print("座位号："+this.seat+"\r\n");
        writer.print("票价："+this.calc()+"元\r\n");
        writer.print("普通票\r\n");
        writer.print("====================感谢您的光临====================\r\n");
        writer.close();
    }
}
